package com.bw.ivideoproject.push

import android.app.AlertDialog
import android.content.ComponentName
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bw.ivideoproject.R
import com.bw.ivideoproject.databinding.FragmentPushBinding
import com.bw.ivideoproject.databinding.ItemActivityBinding
import com.bw.lib_imageutils.ImageLoaderManager
import com.bw.lib_mvvm.BaseFragment
import com.google.android.material.bottomsheet.BottomSheetDialog

class PushFragment : BaseFragment<FragmentPushBinding>() {

    private var bottomSheetDialog: BottomSheetDialog? = null

    override fun bindLayout(): Int {
        return R.layout.fragment_push
    }

    override fun initView() {
        // 初始化视图
    }

    override fun initData() {
        binding.tvPublishTakenow.setOnClickListener {
            showActivitySelectionDialog()
        }

        binding.tvPublishCaogaoxiang.setOnClickListener {
            startActivity(Intent(requireActivity(), CameraVideoActivity::class.java))
        }
    }

    private fun showActivitySelectionDialog() {
        val intent = Intent("com.bw.ivideoproject.push.camera")
        val packageManager = requireActivity().packageManager
        val activities = packageManager.queryIntentActivities(intent, 0)

        if (activities.isNotEmpty()) {
            val customAdapter = CustomAdapter(requireContext(), activities, packageManager)
            bottomSheetDialog = BottomSheetDialog(requireContext()).apply {

                setContentView(R.layout.bottomsheetdialog)
                findViewById<RecyclerView>(R.id.rv)?.apply {
                    adapter = customAdapter
                    layoutManager = GridLayoutManager(requireContext(), 2)
                }
                show()
            }
        } else {
            showMsg("没有匹配的Activity")
        }
    }

    inner class CustomAdapter(
        private val context: Context,
        private val list: List<ResolveInfo>,
        private val packageManager: PackageManager
    ) : RecyclerView.Adapter<CustomAdapter.CustomViewHolder>() {

        inner class CustomViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
            val subBinding = ItemActivityBinding.bind(view)
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.item_activity, parent, false)
            return CustomViewHolder(view)
        }

        override fun getItemCount(): Int {
            return list.size
        }

        override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
            val item = list[position]
            holder.subBinding.textView.text = item.activityInfo.name.substringAfter("com.bw.ivideoproject.push.").removeSuffix("Activity")

            holder.subBinding.root.setOnClickListener {
                val intent = Intent().apply {
                    component = ComponentName(item.activityInfo.packageName, item.activityInfo.name)
                }
                context.startActivity(intent)
                bottomSheetDialog?.dismiss()
            }
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        // 确保在视图销毁时关闭 BottomSheetDialog 以避免内存泄漏
        bottomSheetDialog?.dismiss()
        bottomSheetDialog = null
    }
}
